home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-07  |  5.3 KB  |  206 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** Edit mcc (string subclass with history)
  36. */
  37.  
  38. #include "muihelp.h"
  39. #include "edit.h"
  40.  
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. #include <proto/exec.h>
  45. #include <exec/memory.h>
  46.  
  47. static ULONG edit_new(struct IClass *cl, Object *obj, struct opSet *msg);
  48. static void edit_add(struct editdata *data, char *txt);
  49. static int edit_input(Object *obj, struct editdata *data, struct MUIP_HandleEvent *msg);
  50.  
  51.  
  52. MUI_DISPATCH(edit_dispatch)
  53. {
  54.     struct editdata *data;
  55.  
  56.     switch(msg->MethodID)
  57.     {
  58.     case OM_NEW:
  59.         return edit_new(cl, obj, (APTR)msg);
  60.  
  61.     case OM_DISPOSE:
  62.         data = INST_DATA(cl,obj);
  63.         if(data->pool) DeletePool(data->pool);
  64.         break;
  65.  
  66.     case MUIM_HandleEvent:
  67.         if(edit_input(obj, INST_DATA(cl,obj), (APTR)msg) == 0) break;
  68.         return MUI_EventHandlerRC_Eat;
  69.  
  70.     case MUIM_Textinput_Acknowledge:
  71.         data = INST_DATA(cl,obj);
  72.         if(data->pool) edit_add(data, (char *)MARG1);
  73.         break;
  74.  
  75.     }
  76.     return DoSuperMethodA(cl,obj,msg);
  77. }
  78.  
  79.  
  80. static ULONG edit_new(struct IClass *cl, Object *obj, struct opSet *msg)
  81. {
  82.     struct editdata *data;
  83.  
  84.     obj = (Object *)DoSuperNew(cl, obj,
  85.         StringFrame,
  86.         MUIA_Textinput_AutoExpand, TRUE,
  87.         TAG_MORE, msg->ops_AttrList);
  88.  
  89.     if(!obj) return(0);
  90.  
  91.     data = INST_DATA(cl,obj);
  92.     memset(data, 0, sizeof(struct editdata));
  93.  
  94.     data->type = (int)GetTagData(EDIT_TYPE, 0, msg->ops_AttrList);
  95.  
  96.     if(data->type == EDIT_TYPE_CHAT)
  97.     {
  98.         data->pool = CreatePool(MEMF_PUBLIC, 1024, 900);
  99. /*        if(!data->pool)
  100.         {
  101.             CoerceMethod(cl, obj, OM_DISPOSE);
  102.             return 0;
  103.         }
  104. */        set(obj, MUIA_Textinput_RemainActive, TRUE);
  105.     }
  106.  
  107.     data->tabhook = (editTabHook *)GetTagData(EDIT_TABHOOK, NULL, msg->ops_AttrList);
  108.     data->hookdata = (void *)GetTagData(EDIT_HOOKDATA, NULL, msg->ops_AttrList);
  109.  
  110.  
  111.     return (ULONG)obj;
  112. }
  113.  
  114.  
  115. static void edit_add(struct editdata *data, char *txt)
  116. {
  117.     struct editline *el;
  118.     int len;
  119.  
  120.     if(!txt || *txt == '\0') return;
  121.  
  122.     len = strlen(txt);
  123.     el = AllocPooled(data->pool, sizeof(struct editline) + len + 1);
  124.     if(!el) return;
  125.     memset(el, 0, sizeof(struct editline));
  126.     el->len = len;
  127.     strcpy(&el->text[0], txt);
  128.  
  129.     if(data->lastline)
  130.     {
  131.         data->lastline->next = el;
  132.         el->prev = data->lastline;
  133.     }
  134.     else
  135.     {
  136.         data->lines = el;
  137.     }
  138.     data->lastline = el;
  139.     data->cur = NULL;
  140. }
  141.  
  142.  
  143. static int edit_input(Object *obj, struct editdata *data, struct MUIP_HandleEvent *msg)
  144. {
  145.     u_long t;
  146.     int code, qual;
  147.  
  148.     if(!msg->imsg || msg->imsg->Class != IDCMP_RAWKEY) return(0);
  149.  
  150.     code = msg->imsg->Code;
  151.     qual = msg->imsg->Qualifier;
  152.  
  153.     if(data->tabhook && code == 0x42 && qual == (qual & 0x8004))
  154.     {
  155.         /* lala */
  156.         return 0;
  157.     }
  158.  
  159.     if(data->type == EDIT_TYPE_PLAIN) return(0);
  160.     get(_win(obj), MUIA_Window_ActiveObject, &t);
  161.     if((Object *)t != obj) return(0);
  162.  
  163.     switch(code)
  164.     {
  165.     case 0x4c:
  166.         if(qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  167.         {
  168.             /* shift up arrow */
  169.             data->cur = data->lines;
  170.             set(obj, MUIA_String_Contents, (ULONG) &data->cur->text[0]);
  171.         }
  172.         else
  173.         {
  174.             /* up arrow */
  175.             if(data->cur)
  176.                 data->cur = data->cur->prev;
  177.             else
  178.                 data->cur = data->lastline;
  179.  
  180.             if(data->cur) set(obj, MUIA_String_Contents, (ULONG) &data->cur->text[0]);
  181.         }
  182.         return 1;
  183.  
  184.     case 0x4d:
  185.         if(qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  186.         {
  187.             /* shift down arrow */
  188.             data->cur = NULL;
  189.             set(obj, MUIA_String_Contents, NULL);
  190.         }
  191.         else
  192.         {
  193.             /* down arrow */
  194.             if(data->cur) data->cur = data->cur->next;
  195.  
  196.             if(data->cur)
  197.                 set(obj, MUIA_String_Contents, (ULONG) &data->cur->text[0]);
  198.             else
  199.                 set(obj, MUIA_String_Contents, NULL);
  200.         }
  201.         return 1;
  202.     }
  203.  
  204.     return 0;
  205. }
  206.